#pragma once

// #include <boost/logic/tribool.hpp>
// #include <boost/tuple/tuple.hpp>
#include "../Common/ArrayBuffer.h"
#include <boost/shared_ptr.hpp>
#include "PacketHead.h"

/// Parser for incoming requests.
class PacketParser
{
public:
	/// Construct ready to parse the request method.
	PacketParser();

	/// Reset to initial parser state.
	void reset();


	template <typename InputIterator, typename PackageHandler>
	bool parse(InputIterator begin, InputIterator end, PackageHandler& handler)
	{
		while (begin != end)
		{
			if (consume(*begin++))
			{
				//handle package
				handler(package_head_,m_buffer);
			}
		}

		return true;
	}

private:
	/// Handle the next character of input.
	bool consume(const uint8 input);


	/// The current state of the parser.
	enum state
	{
		package_size_1,
		package_size_2,
		package_cmd_1,
		package_cmd_2,
		package_buffer
	} state_;

	PacketHead package_head_;
	ArrayBuffer m_buffer;


};
